package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppMessageNoticeBo;
import com.ruoyi.app.domain.vo.AppMessageNoticeVo;
import com.ruoyi.app.domain.AppMessageNotice;
import com.ruoyi.app.mapper.AppMessageNoticeMapper;
import com.ruoyi.app.service.IAppMessageNoticeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 消息通知Service业务层处理
 *
 * 提供分页查询、新增、修改、删除等业务方法
 *
 * @author
 * @date 2025-09-29
 */
@RequiredArgsConstructor
@Service
public class AppMessageNoticeServiceImpl implements IAppMessageNoticeService {

    private final AppMessageNoticeMapper baseMapper;

    /**
     * 查询消息通知
     */
    @Override
    public AppMessageNoticeVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询消息通知分页列表
     */
    @Override
    public TableDataInfo<AppMessageNoticeVo> queryPageList(AppMessageNoticeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppMessageNotice> lqw = buildQueryWrapper(bo);
        Page<AppMessageNoticeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询消息通知列表
     */
    @Override
    public List<AppMessageNoticeVo> queryList(AppMessageNoticeBo bo) {
        LambdaQueryWrapper<AppMessageNotice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppMessageNotice> buildQueryWrapper(AppMessageNoticeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppMessageNotice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getReceiverId() != null, AppMessageNotice::getReceiverId, bo.getReceiverId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), AppMessageNotice::getTitle, bo.getTitle());
        lqw.eq(bo.getIsRead() != null, AppMessageNotice::getIsRead, bo.getIsRead());
        return lqw;
    }

    /**
     * 新增消息通知
     */
    @Override
    public Boolean insertByBo(AppMessageNoticeBo bo) {
        AppMessageNotice add = BeanUtil.toBean(bo, AppMessageNotice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消息通知
     */
    @Override
    public Boolean updateByBo(AppMessageNoticeBo bo) {
        AppMessageNotice update = BeanUtil.toBean(bo, AppMessageNotice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppMessageNotice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除消息通知
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
